package cn.youframe.frame.base;

import cn.youframe.frame.beans.BaseVO;
import org.ecside.util.RequestUtil;
import org.ecside.table.limit.Limit;
import org.ecside.table.limit.Sort;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Controler 基类
 */
public class BaseCtrl {
    protected final Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 初始化ECSide
     * @param paraMap
     * @param request
     * @param pageSize
     * @return
     */
    public int initEcSide(Map<String,Object> paraMap,HttpServletRequest request,int pageSize){
        int totalRows =RequestUtil.getTotalRowsFromRequest(request);
        int[] startEnd;
        if (totalRows>0){
            startEnd = RequestUtil.getRowStartEnd(request, totalRows, pageSize,1);
        }else{
            startEnd=new int[]{0,pageSize+1};
        }
        paraMap.put("ecStartEnd",startEnd);
        Limit limit=RequestUtil.getLimit(request, "ec", totalRows,pageSize);
        Sort sort=limit.getSort();
        if(sort!=null && sort.getAlias()!=null){
            paraMap.put("ecSortAlias",sort.getAlias());
            paraMap.put("ecSortType",sort.getSortOrder());
        }
        return totalRows;
    }
    /**
     * 根据Form表单获取页面的元素值
     *
     * @param request
     * @return Map 包含所有的表单值
     */
    protected Map<String, Object> getParamMap(HttpServletRequest request) {
        Map<String, Object> map = null;
        Map<String,String[]> paras=request.getParameterMap();
        if(paras!=null){
            map=new HashMap<String, Object>();
            Iterator<Map.Entry<String,String[]>> paraIt =paras.entrySet().iterator();
            Map.Entry<String,String[]> entry=null;
            while(paraIt.hasNext()){
                entry=paraIt.next();
                if(entry.getValue()!=null && entry.getValue().length==1){
                    map.put(entry.getKey(),entry.getValue()[0]);
                }else{
                    map.put(entry.getKey(),entry.getValue());
                }
            }
        }
        return map;
    }

    /**
     * 设置总行数，适用于Oracle
     * @param records
     * @param request
     * @param pageSize
     * @param <T>
     */
    public <T extends BaseVO> void setTotalRows(List<T> records,HttpServletRequest request,int pageSize) {
        try {
            int totalNums = -1;
            if (records != null && records.size() > 0) {
                T vo = records.get(0);
                totalNums = vo.getTotalNums();
            }
            if(totalNums>0){
                setTotalRows(request,totalNums,pageSize);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * 设置总行数
     * @param request
     * @param totalRows
     * @param pageSize
     */
    public  void setTotalRows(HttpServletRequest request, int totalRows,int pageSize) {
        RequestUtil.getRowStartEnd(request,totalRows,pageSize,1);
    }
    }
